import 'package:flutter/material.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/model/data_model.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/pages/mine/child/download_detail_page.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/chapter_title2_widget.dart';
import 'package:hktk_app/widget/page_title_widget.dart';

/// 我的下载页面
class DownloadPage extends StatefulWidget {
  const DownloadPage({super.key});

  @override
  State<DownloadPage> createState() => _DownloadPageState();
}

class _DownloadPageState extends State<DownloadPage>
    with TickerProviderStateMixin {
  /// tab控制器
  late TabController _tabController;

  /// 获取选择的科目，真正选择的，
  late List<CategoryModel> _categoryList;

  @override
  void initState() {
    super.initState();
    debugPrint("downloadPage 页面 -> initState 方法执行 ===> ");
    debugPrint("");
    _categoryList = IndustryDao.getCategoryModelsSel();
    // tab选中下标
    int index = IndustryDao.getSelectCategoryId() ?? 0;
    _tabController = TabController(
      length: _categoryList.length,
      vsync: this,
      initialIndex: index,
    );
  }

  @override
  void dispose() {
    super.dispose();
    debugPrint("downloadPage 页面 -> dispose 方法执行 ===> ");
    debugPrint("");
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("downloadPage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    return Scaffold(
      backgroundColor: colorWhite2,
      body: Column(
        children: [
          PageTitleWidget(name: "我的资料", pageContext: context),
          hiSpace(height: 30),
          ChapterTitle2Widget(
            tabController: _tabController,
            subList: _categoryList,
            pageName:"/mine/download"
          ),
          hiSpace(height: 30),
          Expanded(child: _content()),
        ],
      ),
    );
  }

  /// 内容显示方法
  _content() {
    DataModel dataModel = dataUtilComm.dataModel;
    return TabBarView(
      controller: _tabController,
      children: _categoryList
          .map((CategoryModel model) => DownloadDetailPage(
        industryName: dataModel.industryName,
        examName: dataModel.examName,
        categoryName: model.name!,
      ))
          .toList(),
    );
  }
}
